/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package entity;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;

/**
 *
 * @author Alessandro
 */
@Entity
public class SentMessage implements Serializable, Comparable{
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected SentMessagePK sentMessagePK;
    @NotNull
    private boolean pending;
    @JoinColumn(name = "message", referencedColumnName = "messageId", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Message message1;
    @JoinColumn(name = "user", referencedColumnName = "email", insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private User user1;

    public SentMessage() {
    }

    public SentMessage(SentMessagePK sentMessagePK) {
        this.sentMessagePK = sentMessagePK;
    }

    public SentMessage(SentMessagePK sentMessagePK, boolean read) {
        this.sentMessagePK = sentMessagePK;
        this.pending = read;
    }

    public boolean isPending() {
        return pending;
    }

    public void setPending(boolean pending) {
        this.pending = pending;
    }

    public Message getMessage1() {
        return message1;
    }

    public void setMessage1(Message message1) {
        this.message1 = message1;
    }

    public User getUser1() {
        return user1;
    }

    public void setUser1(User user1) {
        this.user1 = user1;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (sentMessagePK != null ? sentMessagePK.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof SentMessage)) {
            return false;
        }
        SentMessage other = (SentMessage) object;
        if ((this.sentMessagePK == null && other.sentMessagePK != null) || (this.sentMessagePK != null && !this.sentMessagePK.equals(other.sentMessagePK))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entity.SentMessage[ sentMessagePK=" + sentMessagePK + " ]";
    }

    @Override
    public int compareTo(Object o) {
        SentMessage first = (SentMessage) o;
        if (first.getMessage1().getTimestamp().before(this.message1.getTimestamp())) {
            return -1;
        } else {
            if (first.getMessage1().getTimestamp().after(this.message1.getTimestamp())) {
                return +1;
            } else {
                return 0;
            }
        }
    }
    
}
